﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace AcademicAudit_JoelShuman
{
    public static class StudentIDNumbers
	{
        public static String GetUniqueID()
		{
			char[] trimChars = { '\n', 'r', ' ' };

			// Generate unique courseID; 
			FileStream tempFs = new FileStream("../../DataFiles/StudentID.txt", FileMode.Open, FileAccess.Read);
			StreamReader tempReader = new StreamReader(tempFs);
			String tempLine = tempReader.ReadLine().TrimEnd(trimChars);
			tempReader.Close();
			tempFs.Close();

			int nextIdAvailable = Convert.ToInt32(tempLine);
			m_RevID = nextIdAvailable;

			tempFs = new FileStream("../../DataFiles/StudentID.txt", FileMode.Truncate, FileAccess.Write);
			StreamWriter tempWriter = new StreamWriter(tempFs);
			tempWriter.WriteLine(++nextIdAvailable);
			tempWriter.Close();
			tempFs.Close();

            return m_MajorID.ToString() + "-" + m_MinorID.ToString("D3") + "-" + m_RevID++.ToString("D4");
        }

		public static bool SetUniqueID( int major, int minor, int rev )
		{
			if (rev <= m_RevID)
				return false;

			m_MajorID = major;
			m_MinorID = minor;
			m_RevID = rev;

			return true;
		}
		
        private static int m_MajorID = 0;
        private static int m_MinorID = 0;
        private static int m_RevID = 0;
    }
}
